function oldWay(e: string) {
  const t = document.createElement('textarea')
  const n = document.activeElement as typeof t
  t.value = e
  t.setAttribute('readonly', '')
  t.style.contain = 'strict'
  t.style.position = 'absolute'
  t.style.left = '-9999px'
  t.style.fontSize = '12pt'
  const o = document.getSelection()
  const s = o ? o.rangeCount > 0 && o.getRangeAt(0) : null
  document.body.appendChild(t)
  t.select()
  t.selectionStart = 0
  t.selectionEnd = e.length
  document.execCommand('copy')
  document.body.removeChild(t)
  s && (o!.removeAllRanges(), o!.addRange(s)), n && n.focus()
}

/**
 * 通过点击方式进行复制
 * @param text 复制文本
 * @returns
 */
async function clipboardCopyByClickHandler(text: string) {
  if (navigator.clipboard) {
    return navigator.clipboard.writeText(text).catch(() => {
      oldWay(text)
    })
  } else {
    return oldWay(text)
  }
}
export { clipboardCopyByClickHandler }
